package com.loopy.homework;

import com.loopy.cookie.CookieUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("LoginServlet接口被调用");
        Cookie[] cookies = request.getCookies();
        Cookie cookie1 = CookieUtils.readCookieByName("username", cookies);
        String username="";
        if(cookie1!=null){
            username= cookie1.getValue();
        }
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h1>用户登录界面</h1>\n" +
                "<form action=\"/cookie/judge\" method=\"get\">\n" +
                "  用户名:<input type=\"text\" value=\""+username+"\"name=\"username\"><br>\n" +
                "  密码:<input type=\"password\" name=\"pwd\"><br>\n" +
                "  <input type=\"submit\" value=\"登录\">\n" +
                "</form>\n" +
                "</body>\n" +
                "</html>");
        writer.flush();
        writer.close();

    }
}
